package com.tianyang.yukuaifuzs.business.auth.presenter;

import com.tianyang.yukuaifuzs.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifuzs.business.auth.contract.BusinessInfoContract;
import com.tianyang.yukuaifuzs.business.auth.model.AuthModel;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：行业类别
 * Created by xyj on 2024/3/14
 */
public class BusinessInfoPresenter implements BusinessInfoContract.Presenter {

    private BusinessInfoContract.View mView;

    private CompositeSubscription mSubscription;

    private String merchantType, mccType, channel;

    public BusinessInfoPresenter(BusinessInfoContract.View mView, String merchantType, String mccType, String channel) {
        this.mView = mView;
        this.merchantType = merchantType;
        this.mccType = mccType;
        this.channel = channel;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getMcc(final int pageNumber, String keyword, String area) {
        Subscription subscription = AuthModel.getInstance().getMcc(pageNumber, keyword, area, merchantType, mccType, channel)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<BusinessOutput>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showMccView(null, pageNumber == CommonConfig.PAGE_NUM);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(List<BusinessOutput> outputs) {
                        mView.showMccView(outputs, pageNumber == CommonConfig.PAGE_NUM);
                        if (outputs != null) {
                            if (outputs.size() < CommonConfig.PAGE_SIZE_2) {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                            }
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
